package com.lagou.server;

import com.lagou.model.TestPaper;
import com.lagou.model.TestScore;
import com.lagou.model.User;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Consumer;

public class ServerTestPaperDaoImpl implements ServerTestPaperDao {

    private List<TestPaper>testPaperList = new ArrayList();
    private Map<Integer,Boolean> grades = new HashMap<>();
    private TestScore testScore;
    private List<TestScore> testScoreList = new ArrayList<>();

    private ObjectInputStream objectInputStream ;

    {
        try {
            File file = new File("D:/TestPaper.txt");
            if (file.length()!=0){
                objectInputStream = new ObjectInputStream(new FileInputStream("D:/TestPaper.txt"));
                testPaperList = (List<TestPaper>) objectInputStream.readObject();
            }else{
                testPaperList =  new ArrayList<>();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (EOFException  e){
             e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            File file = new File("D:/TestScore.txt");
            if (file.length()!=0){
                objectInputStream = new ObjectInputStream(new FileInputStream("D:/TestScore.txt"));
                testScoreList = (List<TestScore>) objectInputStream.readObject();
            }else{
                testScoreList =  new ArrayList<>();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (EOFException  e){
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean ServerInsertTestPaper(TestPaper testPaper) {
            if (null!=testPaper || testPaperList.size()<1  ){
                testPaperList.add(testPaper);
                return true;
            }else {
                for (TestPaper paper : testPaperList) {
                    if (!paper.getTopic().equals(testPaper.getTopic())){
                        testPaperList.add(testPaper);
                        return true;
                    }
                }
            }
            return false;
    }

    @Override
    public boolean ServerRemoveTestPaper(String topic) {
        if (testPaperList.size()<1 || null ==topic){
            return false;
        }else {
            TestPaper testPaper = ServerSelectTestPaperByTopic(topic);
            if (null != testPaper){
                testPaperList.remove((testPaper));
                return true;
            }
            return false;
        }
    }

    @Override
    public TestPaper ServerSelectTestPaperByTopic(String topic) {
        if (null!=topic && testPaperList.size()>0){
            for (TestPaper testPaper :testPaperList){
                if (testPaper.getTopic().equals(topic)){
                    return testPaper;
                }
            }
        }
        return null;
    }

    @Override
    public List<TestPaper> ServerSelectAllTestPaper() {
        if (testPaperList.size()<0 || null ==testPaperList ){
            return null;
        }
        return testPaperList;
    }

    @Override
    public boolean ServerUpdateTestPaper(String topic, TestPaper testPaper) {
        if (null!=testPaper){
            for (int i = 0; i < testPaperList.size(); i++) {
                if (testPaperList.get(i).getTopic().equals(topic)){
                    testPaperList.get(i).setTopic(testPaper.getTopic());
                    testPaperList.get(i).setA(testPaper.getA());
                    testPaperList.get(i).setB(testPaper.getB());
                    testPaperList.get(i).setC(testPaper.getC());
                    testPaperList.get(i).setD(testPaper.getD());
                    testPaperList.get(i).setCorrectAnswer(testPaper.getCorrectAnswer());
                    System.out.println(testPaperList.get(i).toString());
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean ServerImportTestPager(String path) throws IOException, ClassNotFoundException {
         List<TestPaper>testPaperList1 = new ArrayList();
        File file = new File(path);
        if (file.length()!=0) {
            objectInputStream = new ObjectInputStream(new FileInputStream(path));
            testPaperList1 = (List<TestPaper>) objectInputStream.readObject();

            for (int i = 0; i < testPaperList1.size(); i++) {
                for (int j = 0; j < testPaperList.size(); j++) {
                    if (testPaperList1.get(i).getTopic().equals(testPaperList.get(j).getTopic())){
                       break;
                    }
                }
                testPaperList.add(testPaperList1.get(i));
            }
            return true;
        }
       return false;
    }


    @Override
    public void ServerTestAnswerCheck(TestScore testScore) {
        //记录没道题的结果
        List<Map<Integer,Boolean>> mapList = new ArrayList<>();
        //存放 考试时间+考试结果  List<Map<Integer,Boolean>>=每道题结果的集合
        Map<String, List<Map<Integer,Boolean>>> testScoreMap = new HashMap<>();
        //取出 用户所答的考题对结果进行校验
        List<TestPaper> testPaperList1 = testScore.getListTestPaper();
        //记录当前考试时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String examinationTime = simpleDateFormat.format(date);

        //将用户的每道题题目 和 系统的每一道题 题目进行比较
        for (int i = 0; i < testPaperList.size(); i++) {
            for (int j=0;j<testPaperList1.size();j++){
                //先判断题目 是否相等
                if (testPaperList1.get(i).getTopic().equals(testPaperList.get(j).getTopic())){
                    //题目相等的情况下 对正确答案进行判断   答案相等 则 说明该题答对了 否则错误 map集合记录 题目编号 和结果
                    if (testPaperList1.get(i).getCorrectAnswer().equals(testPaperList.get(j).getCorrectAnswer())){
                        grades.put(i,true);
                        mapList.add(grades);
                    }else {
                        grades.put(i,false);
                        mapList.add(grades);
                    }
                }
            }

        }
        testScoreMap.put(examinationTime,mapList);
        testScore.setTestScoreList(testScoreMap);
        testScoreList.add(testScore);

    }

    @Override
    public TestScore ServerTestAnswerSelect(TestScore testScore) {
        User user = testScore.getUser();
        for (int i = 0; i < testScoreList.size(); i++) {
            if (user.getStudentId().equals(testScoreList.get(i).getUser().getStudentId())){
                return testScoreList.get(i);
            }
        }
        return null;
    }

    /**
     * D:\studentResultByStudentId.txt
     * @param studentId
     * @param path
     * @return
     * @throws IOException
     */
    public boolean ServerExportResultByStudentId(String studentId,String path) throws IOException {
        for (int i = 0; i < testScoreList.size(); i++) {
            if (testScoreList.get(i).getUser().getStudentId().equals(studentId)){
                File file = new File(path);
                if(!file.exists()){
                    file.createNewFile();
                }
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));
                objectOutputStream.writeObject(testScoreList.get(i).getTestScoreList());
                return true;
            }
        }
        return false;
    }

    /**
     * 退出时将数据保存至文件
     * @throws IOException
     */
    public void closeSavaData() throws IOException {
        File file = new File("D:/TestPaper.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:/TestPaper.txt"));
        objectOutputStream.writeObject(testPaperList);

        File file1 = new File("D:/TestScore.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:/TestScore.txt"));
        objectOutputStream.writeObject(testScoreList);
    }

}
